<?php

class Home extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('setting_model');
    }

    public function index() {

        $obj_autopart = $this->list_autopart();
        $this->data['list_autopart'] = $obj_autopart['list'];
        $this->data['count_autopart'] = $obj_autopart['count'];

        $obj_truck = $this->list_truck();
        $this->data['list_truck'] = $obj_truck['list'];
        $this->data['count_truck'] = $obj_truck['count'];
        $this->load->view('setting/index', $this->data);
    }

    private function list_autopart() {
        $arrMainType = $this->setting_model->get_list_autopart_main_type();
        $str_list = '';
        $count_autopart = 0;
        foreach ($arrMainType as $main_obj) {
            $str_list .= '<li><h4><span id="liAutoPart' . $count_autopart . '" auto_type="main_type" value="' . $main_obj->id . '" class="label label-info" style="cursor:pointer">' . $main_obj->name . ' </span></h4></a>';
            $str_list .= '<ul>';
            $count_autopart = $count_autopart + 1;
            $arrSubType = $this->setting_model->get_list_autopart_sub_type($main_obj->id);
            foreach ($arrSubType as $sub_obj) {
                $str_list .= '<li><h4><span id="liAutoPart' . $count_autopart . '" auto_type="sub_type" value="' . $sub_obj->id . '" class="label label-success" style="cursor:pointer">' . $sub_obj->name . ' </span></h4></a>';
                $str_list .= '<ul>';
                $count_autopart = $count_autopart + 1;
                $arrBrand = $this->setting_model->get_list_autopart_brand($sub_obj->id);
                foreach ($arrBrand as $brand_obj) {
                    $str_list .= '<li><h4><span id="liAutoPart' . $count_autopart . '" auto_type="brand" value="' . $brand_obj->id . '" class="label label-warning" style="cursor:pointer">' . $brand_obj->name . ' </span></h4></a>';
                    $count_autopart = $count_autopart + 1;
                }
                $str_list .= '</ul>';
            }
            $str_list .= '</ul>';
            $str_list .= '</li>';
        }
        $array['list'] = $str_list;
        $array['count'] = $count_autopart;
        return $array;
    }

    private function list_truck() {
        $arrTBrand = $this->setting_model->get_list_truck_brand();
        $str_list = '';
        $count_truck = 0;
        foreach ($arrTBrand as $brand_obj) {
            $str_list .= '<li><h4><span id="liTruck' . $count_truck . '" truck_type="brand" value="' . $brand_obj->id . '" class="label label-info" style="cursor:pointer">' . $brand_obj->name . ' </span></h4></a>';
            $str_list .= '<ul>';
            $count_truck = $count_truck + 1;
            $arrTModel = $this->setting_model->get_list_truck_model($brand_obj->id);
            foreach ($arrTModel as $model_obj) {
                $str_list .= '<li><h4><span id="liTruck' . $count_truck . '" truck_type="model" value="' . $model_obj->id . '" class="label label-success" style="cursor:pointer">' . $model_obj->name . ' </span></h4></a>';
                $count_truck = $count_truck + 1;
            }
            $str_list .= '</ul>';
            $str_list .= '</li>';
        }
        $array['list'] = $str_list;
        $array['count'] = $count_truck;
        return $array;
    }

    public function get_autopart_type() {
        $id = $this->input->post('id');
        $result = $this->setting_model->get_autopart_type_by_id($id);
        $array['name'] = $result[0]->name;
        $array['parent_name'] = $result[0]->parent_name;
        echo json_encode($array);
    }

    public function get_truck_type() {
        $id = $this->input->post('id');
        $result = $this->setting_model->get_truck_type_by_id($id);
        $array['name'] = $result[0]->name;
        $array['parent_name'] = $result[0]->parent_name;
        echo json_encode($array);
    }

    public function save_autoparts() {
        $parent_id = $this->input->post('parent_id');
        $type_name = $this->input->post('type_name');
        $this->setting_model->save_autopart_type($parent_id === '' ? NULL : $parent_id, $type_name);
        $obj_autopart = $this->list_autopart();
        $data['list_autopart'] = $obj_autopart['list'];
        $data['count_autopart'] = $obj_autopart['count'];
        echo json_encode($data);
    }

    public function update_autoparts() {
        $id = $this->input->post('id');
        $type_name = $this->input->post('type_name');
        $this->setting_model->update_autopart_type($id, $type_name);
        $obj_autopart = $this->list_autopart();
        $data['list_autopart'] = $obj_autopart['list'];
        $data['count_autopart'] = $obj_autopart['count'];
        echo json_encode($data);
    }

    public function save_truck_type() {
        $parent_id = $this->input->post('parent_id');
        $type_name = $this->input->post('type_name');
        $this->setting_model->save_truck_type($parent_id === '' ? NULL : $parent_id, $type_name);
        $obj_truck = $this->list_truck();
        $data['list_truck'] = $obj_truck['list'];
        $data['count_truck'] = $obj_truck['count'];
        echo json_encode($data);
    }

    public function update_truck_type() {
        $id = $this->input->post('id');
        $type_name = $this->input->post('type_name');
        $this->setting_model->update_truck_type($id, $type_name);
        $obj_truck = $this->list_truck();
        $data['list_truck'] = $obj_truck['list'];
        $data['count_truck'] = $obj_truck['count'];
        echo json_encode($data);
    }

    public function delete_autopart() {
        $id = $this->input->post('id');
        $this->setting_model->delete_autopart($id);
        $obj_autopart = $this->list_autopart();
        $data['list_autopart'] = $obj_autopart['list'];
        $data['count_autopart'] = $obj_autopart['count'];
        echo json_encode($data);
    }

    public function delete_truck_type() {
        $id = $this->input->post('id');
        $this->setting_model->delete_truck_type($id);
         $obj_truck = $this->list_truck();
        $data['list_truck'] = $obj_truck['list'];
        $data['count_truck'] = $obj_truck['count'];
        echo json_encode($data);
    }

}
